package com.hauwei.huaweishop.controller;

import com.hauwei.huaweishop.entity.ShopTypes;
import com.hauwei.huaweishop.service.ShopTypesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/shoptypes")
public class ShopTypesController {
    @Autowired
    private ShopTypesService shopTypesService;

    /**
     * 查找所有信息
     * @return
     */
    @GetMapping("/findAllShopTypes")
    public Map<String, Object> findAll(){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            // 查询员工的信息
            List<ShopTypes> shopTypesList = shopTypesService.queryShopTypesList();
            resultMap.put("records", shopTypesList);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品类型查询成功");
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品类型查询失败");
        }
        return resultMap;
    }

    /**
     * 通过id查询信息
     * @param id
     * @return
     */
    @GetMapping("/findOneShopTyepsById")
    public Map<String, Object> findOneById(Integer id){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            ShopTypes shopTypesById = shopTypesService.queryBySid(id);
            resultMap.put("recorde", shopTypesById);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品类型查询成功");
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品类型查询失败");
        }
        return resultMap;
    }

    /**
     * 通过名称查询信息
     * @param name
     * @return
     */
    @GetMapping("/findOneByShopTypesName")
    public Map<String, Object> findOneByName(String name){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            ShopTypes queryBySname = shopTypesService.queryBySname(name);
            resultMap.put("recorde", queryBySname);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品类型查询成功");
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品类型查询失败");
        }
        return resultMap;
    }

    /**
     * 添加方法
     * @param shopTypes
     * @return
     */
    @PostMapping("/addShopTypes")
    public Map<String, Object> addShopTypes(ShopTypes shopTypes){
        Map<String, Object> resultMap = new HashMap<>();
        log.info("商品类型信息：[{}]", shopTypes);
        try {
            // 新增信息
            shopTypesService.addShopTypes(shopTypes);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品类型增加成功");
        }catch (Exception e){
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品类型增加失败");
        }
        return resultMap;
    }

    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @GetMapping("/deletShopTypesById")
    public Map<String, Object> deletShopTypesById(Integer id){
        Map<String, Object> resultMap = new HashMap<>();
        log.info("删除信息id：[{}]", id);
        try {
            // 删除信息
            shopTypesService.deleteShopTypesBySid(id);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品类型删除成功");
        }catch (Exception e){
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品类型删除失败");
        }
        return resultMap;
    }

    /**
     * 根据name删除信息
     * @param name
     * @return
     */
    @GetMapping("/deletShopTypesByName")
    public Map<String, Object> deletShopTypesByName(String name){
        Map<String, Object> resultMap = new HashMap<>();
        log.info("删除信息name：[{}]", name);
        try {
            // 删除信息
            shopTypesService.deleteShopTypesBySname(name);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品类型删除成功");
        }catch (Exception e){
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品类型删除失败");
        }
        return resultMap;
    }

    /**
     * 修改信息
     * @param shopTypes
     * @return
     */
    @RequestMapping("/updateShopTypes")
    public Map<String, Object> updateShopTypes(ShopTypes shopTypes){
        Map<String, Object> resultMap = new HashMap<>();
        log.info("修改信息：[{}]", shopTypes);
        try {
            // 修改信息
            shopTypesService.updateShopTypes(shopTypes);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品类型修改成功");
        }catch (Exception e){
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品类型修改失败");
        }
        return resultMap;
    }
}
